package o2o.web.actions.multilingual;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ValidateUtil;
import net.sf.json.JSONObject;
import o2o.cache.order.OrderStatusNameMemory;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.common.ResultInfo;
import o2o.dao.PO.order.OrderStatusName;
import o2o.service.multilingual.OrderStatusNameService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qilonghe【1061268464@qq.com】
 * @ClassName OrderStatusNameController
 * @Description:
 * @date 2020/11/12 0012 上午 11:33
 * @Version 1.0
 */
// 订单状态多语言
@RestController
public class OrderStatusNameController {

    @Autowired
    OrderStatusNameService orderStatusNameService;

    // 列表
    @RequestMapping(method = RequestMethod.POST, value = "/base/multilingual/list")
    public String list(Integer page, Integer size, String param) {
        List<OrderStatusName> list = orderStatusNameService.list(param);

        if (page == null && size == null) {
            return AjaxResult.successResult(list, list.size());
        } else {
            if (page == null) {
                page = 1;
            }
            if (size == null) {
                size = 20;
            }
            if (ValidateUtil.isAbsList(list)) {
                JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
                if (ValidateUtil.isAbsList(pageList.list)) {
                    return AjaxResult.successResult(pageList.list, pageList.totalCount);
                }
            }
        }

        return AjaxResult.successResult(null, 0);
    }

    // 新增
    @RequestMapping(method = RequestMethod.POST, value = "/base/multilingual/add")
    public String add(OrderStatusName orderStatusName) {
        if (orderStatusName == null) {
            return AjaxResult.errorResult("请求参数不能为空");
        }
        if (StringUtils.isBlank(orderStatusName.getName())) {
            return AjaxResult.errorResult("名称不能为空");
        }
        if (null == orderStatusName.getOrderStatus()) {
            return AjaxResult.errorResult("订单状态不能为空");
        }
        if (StringUtils.isBlank(orderStatusName.getDescs())) {
            return AjaxResult.errorResult("描述不能为空");
        }
        try {
            orderStatusNameService.add(orderStatusName);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("添加失败,请稍重试");
        }

        return AjaxResult.successResult();
    }

    // 修改
    @RequestMapping(method = RequestMethod.POST, value = "/base/multilingual/modify")
    public String modify(OrderStatusName orderStatusName) {
        if (orderStatusName == null) {
            return AjaxResult.errorResult("请求参数不能为空");
        }
        if (null == orderStatusName.getId()) {
            return AjaxResult.errorResult("主键不能为空");
        }
        try {
            orderStatusNameService.modify(orderStatusName);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败,请稍重试");
        }
        return AjaxResult.successResult();
    }

    // 删除
    @RequestMapping(method = RequestMethod.POST, value = "/base/multilingual/delete")
    public String delete(Long id) {
        if (null == id) {
            return AjaxResult.errorResult("主键不能为空");
        }
        try {
            orderStatusNameService.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败,请稍重试");
        }
        return AjaxResult.successResult();
    }

    // excel导入
    @RequestMapping(method = RequestMethod.POST, value = "/base/multilingual/excelImport")
    public String excelImport(MultipartFile file) {
        if (null == file) {
            return AjaxResult.errorResult("文件不能为空");
        }
        Connection con = null;
        try {
            con = UserDaoSql.getInstance().getConnection();
            con.setAutoCommit(false);
            ResultInfo resultInfo = orderStatusNameService.excelImport(file, con);

            if (resultInfo.isSuccess()) {
                con.commit();
                OrderStatusNameMemory.getInstance().run();
                return AjaxResult.successResult();
            }
            UserDaoSql.getInstance().rollback(con);

            Map<String, String> map = new HashMap<>();
            map.put("error", resultInfo.getError());
            StringBuffer buffer = new StringBuffer();
            buffer.append("{\"status\":0,")
                    .append("\"type\":\"comError\",\"error\":")
                    .append(JSONObject.fromObject(map))
                    .append("}");
            return buffer.toString();

        } catch (Exception e) {
            e.printStackTrace();
            UserDaoSql.getInstance().rollback(con);
            return AjaxResult.errorResult("修改失败,请稍重试");
        } finally {
            UserDaoSql.getInstance().close(con);
        }
    }
}